package com.zjy.org.controller;

import com.zjy.basic.annotation.PreAuthorize;
import com.zjy.basic.util.PageList;
import com.zjy.basic.util.AjaxResult;
import com.zjy.org.domain.Department;
import com.zjy.org.query.DepartmentQuery;
import com.zjy.org.service.DepartmentService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/department")
//接口文档的注解
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/{id}")
    @PreAuthorize(name = "查询单个部门",value="department:getOne")
    public Department findById(@PathVariable("id") Long id) {
        return departmentService.findById(id);
    }

    @GetMapping
    @PreAuthorize(name = "查询所有部门",value="department:getAll")
    public List<Department> findAll() {
        return departmentService.findAll();
    }

    @PutMapping
    @PreAuthorize(name = "添加和修改部门",value="department:save")
    public AjaxResult save(@RequestBody Department department) {
            if (department.getId() == null) {
                departmentService.add(department);
            } else {
                departmentService.update(department);
            }
            return AjaxResult.me();
    }

    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除部门",value="department:delete")
    public AjaxResult del(@PathVariable("id") Long id) {
            departmentService.del(id);
            return AjaxResult.me();
    }

    /**
     * 分页查询
     * @param departmentQuery
     * @return
     */
    @PostMapping
    @PreAuthorize(name = "查询部门列表",value="department:list")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @PreAuthorize(name = "批量删除部门",value="department:patchDel")
    public AjaxResult patchDel(@RequestBody Long[] ids) {
            departmentService.patchDel(ids);
            return AjaxResult.me();
    }

    /**
     * 部门树
     * @return
     */
    @GetMapping("/deptTree")
    @PreAuthorize(name = "查询部门树",value="department:deptTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }
}
